home *** CD-ROM | disk | FTP | other *** search
- #include <Types.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <SegLoad.h>
- #include <Files.h>
- #include <OSUtils.h>
- #include <OSEvents.h>
- #include <DiskInit.h>
- #include <Packages.h>
- #include <Traps.h>
- #include <SoundInput.h>
- #include <QDOffscreen.h>
- #include <SysEqu.h>
-
- #include "Sample.h" /* bring in all the #defines for Sample */
-
-
- Boolean gInBackground; /* maintained by Initialize and DoEvent */
- long gInRefNum;
- short lastValue = 0;
- GWorldPtr myGWorld;
- long gLastDown;
- Boolean down = false;
- short stringCount = 1;
- Rect gGoRect, gStopRect;
-
- #pragma segment Main
- main()
- {
- UnloadSeg((Ptr) _DataInit); /* note that _DataInit must not be in Main! */
-
- MaxApplZone(); /* expand the heap so code segments load at the top */
-
- Initialize(); /* initialize the program */
- UnloadSeg((Ptr) Initialize); /* note that Initialize must not be in Main! */
-
- EventLoop(); /* call the main event loop */
-
- }
-
-
- #pragma segment Main
- void EventLoop()
- {
- Boolean gotEvent;
- EventRecord event;
-
- do {
- /* use WNE if it is available */
- gotEvent = WaitNextEvent(everyEvent, &event, 0L, nil);
- if ( gotEvent ) {
- DoEvent(&event);
- }
- DoAnalysis(gInRefNum);
- } while ( true ); /* loop forever; we quit via ExitToShell */
- } /*EventLoop*/
-
-
- #pragma segment Main
- void DoEvent(event)
- EventRecord *event;
- {
- short part;
- WindowPtr window;
- Boolean hit;
- char key;
-
- switch ( event->what ) {
- case mouseDown:
- part = FindWindow(event->where, &window);
- switch ( part ) {
- case inMenuBar: /* process a mouse menu command (if any) */
- AdjustMenus();
- DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: /* let the system handle the mouseDown */
- SystemClick(event, window);
- break;
- case inContent:
- if ( window != FrontWindow() ) {
- SelectWindow(window);
- /*DoEvent(event);*/ /* use this line for "do first click" */
- } else
- DoContentClick(window);
- break;
- case inDrag: /* pass screenBits.bounds to get all gDevices */
- DragWindow(window, event->where, &qd.screenBits.bounds);
- break;
- case inGrow:
- break;
- case inZoomIn:
- case inZoomOut:
- hit = TrackBox(window, event->where, part);
- if ( hit ) {
- SetPort(window); /* the window must be the current port... */
- EraseRect(&window->portRect); /* because of a bug in ZoomWindow */
- ZoomWindow(window, part, true); /* note that we invalidate and erase... */
- InvalRect(&window->portRect); /* to make things look better on-screen */
- }
- break;
- }
- break;
- case keyDown:
- case autoKey: /* check for menukey equivalents */
- key = event->message & charCodeMask;
- if ( event->modifiers & cmdKey ) /* Command key down */
- if ( event->what == keyDown ) {
- AdjustMenus(); /* enable/disable/check menu items properly */
- DoMenuCommand(MenuKey(key));
- }
- break;
- case activateEvt:
- DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) event->message);
- break;
- case osEvt:
- switch ((event->message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (event->message & resumeFlag ) == 0;
- DoActivate(FrontWindow(), !gInBackground);
- break;
- }
- break;
- }
- } /*DoEvent*/
-
- #pragma segment Main
- void DoUpdate(window)
- WindowPtr window;
- {
- BeginUpdate(window); /* this sets up the visRgn */
- if ( ! EmptyRgn(window->visRgn) ) /* draw if updating needs to be done */
- DrawWindow(window);
- EndUpdate(window);
- } /*DoUpdate*/
-
- #pragma segment Main
- void DoActivate(window, becomingActive)
- WindowPtr window;
- Boolean becomingActive;
- {
- if ( becomingActive )
- /* do whatever you need to at activation */ ;
- else
- /* do whatever you need to at deactivation */ ;
- } /*DoActivate*/
-
-
- #pragma segment Main
- void DoContentClick(window)
- WindowPtr window;
- {
- } /*DoContentClick*/
-
-
- #pragma segment Main
- void DrawWindow(window)
- WindowPtr window;
- {
- SetPort(window);
-
- EraseRect(&window->portRect); /* clear out any garbage that may linger */
- } /*DrawWindow*/
-
- #pragma segment Main
- void DoMenuCommand(menuResult)
- long menuResult;
- {
- short menuID; /* the resource ID of the selected menu */
- short menuItem; /* the item number of the selected menu */
- short itemHit;
- Str255 daName;
- short daRefNum;
- Boolean handledByDA;
-
- menuID = HiWord(menuResult); /* use macros for efficiency to... */
- menuItem = LoWord(menuResult); /* get menu item number and menu number */
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout: /* bring up alert for About */
- itemHit = Alert(rAboutAlert, nil);
- break;
- default: /* all non-About items in this menu are DAs */
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem ) {
- case iClose:
- DoCloseWindow(FrontWindow());
- break;
- case iQuit:
- Terminate();
- break;
- }
- break;
- case mEdit: /* call SystemEdit for DA editing & MultiFinder */
- handledByDA = SystemEdit(menuItem-1); /* since we don’t do any Editing */
- break;
- }
- HiliteMenu(0); /* unhighlight what MenuSelect (or MenuKey) hilited */
- } /*DoMenuCommand*/
-
-
- #pragma segment Main
- Boolean DoCloseWindow(window)
- WindowPtr window;
- {
- CloseWindow(window);
- return true;
- } /*DoCloseWindow*/
-
- #pragma segment Main
- void Terminate()
- {
- WindowPtr aWindow;
- Boolean closed;
- FInfo theInfo;
-
- closed = true;
-
- GetFInfo("\pBrita", 0, &theInfo);
- theInfo.fdFlags -= fInvisible;
- SetFInfo("\pBrita", 0, &theInfo);
- CloseDevice(gInRefNum);
-
- do {
- aWindow = FrontWindow(); /* get the current front window */
- if (aWindow != nil)
- closed = DoCloseWindow(aWindow); /* close this window */
- }
- while (closed && (aWindow != nil));
- if (closed)
- ExitToShell(); /* exit if no cancellation */
- } /*Terminate*/
-
- #pragma segment Initialize
- void Initialize()
- {
- Handle menuBar;
- WindowPtr window;
- EventRecord event;
- short count;
- OSErr err;
- short i;
- CGrafPtr port, currPort;
- GDHandle gdh, currDevice;
- Rect picRect;
-
- gInBackground = false;
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- for (count = 1; count <= 3; count++)
- EventAvail(everyEvent, &event);
-
- window = GetNewCWindow(rWindow, nil, (WindowPtr) -1);
- SetPort(window);
- TextFont(times);
- TextSize(18);
-
- SetRect(&gStopRect, 270, 10, 370, 110);
- SetRect(&gGoRect, 270, 120, 370, 220);
-
- if ( down ) /* draw a red (or white) stop light */
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gStopRect);
- ForeColor(blackColor);
- FrameOval(&gStopRect);
- if ( ! down ) /* draw a green (or white) go light */
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gGoRect);
- ForeColor(blackColor);
- FrameOval(&gGoRect);
-
- ValidRect(&(window->portRect));
-
- SetRect(&picRect,0,0,250*40,150);
- err = NewGWorld(&myGWorld,8,&picRect,0L,0L,0);
- LockPixels(GetGWorldPixMap(myGWorld));
-
- GetGWorld(&currPort, &currDevice);
- SetGWorld(myGWorld,0L);
-
- SetRect(&picRect,0,0,250,150);
- for (i = 0;i <= 32;i++)
- {
- PicHandle pic;
-
- pic = (PicHandle)GetResource('PICT',i+128);
- if (pic)
- {
- DrawPicture(pic,&picRect);
- OffsetRect(&picRect, 250, 0);
- }
- ReleaseResource((Handle) pic);
- }
-
- SetGWorld(currPort,currDevice);
-
-
- menuBar = GetNewMBar(rMenuBar); /* read menus into menu bar */
- SetMenuBar(menuBar); /* install menus */
- DisposHandle(menuBar);
- AddResMenu(GetMHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
- DrawMenuBar();
-
- err = TurnOnContinuousRecordingAndLevelMetering(&gInRefNum);
-
- } /*Initialize*/
-
-
- #pragma segment Main
- void AlertUser()
- {
- short itemHit;
-
- SetCursor(&qd.arrow);
- itemHit = Alert(rUserAlert, nil);
- ExitToShell();
- } /* AlertUser */
-
- void AdjustMenus( void )
- {
-
- }
-
- OSErr TurnOnContinuousRecordingAndLevelMetering(long *inRefNum)
- {
- SPB pb;
- OSErr err;
- Str255 deviceName;
- short array[12];
-
- if (err = SPBOpenDevice( 0L, siWritePermission, inRefNum)) return err;
- if (err = SPBSetDeviceInfo(*inRefNum, siContinuous, (Ptr)array)) return err;
- array[0] = 1;
- array[1] = 0;
- if (err = SPBSetDeviceInfo(*inRefNum, siLevelMeterOnOff, (Ptr)array)) return err;
-
- return err;
- }
-
- OSErr CloseDevice(long inRefNum)
- {
- OSErr err;
-
- err = SPBCloseDevice( inRefNum);
- return err;
- }
-
- short GetMeterValue(long inRefNum)
- {
- SPB pb;
- OSErr err;
- short array[12];
-
- if (err = SPBGetDeviceInfo(inRefNum, siLevelMeterOnOff, (Ptr)array)) return err;
- return (array[1]);
- }
-
- void DoAnalysis(long inRefNum)
- {
- short value;
- Handle thepic;
- Rect theRect;
- Str255 numstr;
- Str255 theString;
-
- value = GetMeterValue(inRefNum);
- value /= 8;
- if (lastValue + 3 < value)
- {
- lastValue+= 3;
- }
- else if (lastValue - 3 > value)
- {
- lastValue -= 3;
- }
- else
- lastValue = value;
-
- if (lastValue < 0)
- lastValue = 0;
-
- if (lastValue < 8)
- {
- lastValue = 8;
- if ( (!down) && ((TickCount() - gLastDown) > 90))
- {
- if (stringCount < 13) {
- GetIndString(theString, 128, stringCount++);
- SetRect(&theRect, 10, 170, 260, 300);
- TextBox(&(theString[1]), theString[0], &theRect, teJustLeft);
- }
- else
- SysBeep(1);
- down = true;
- if ( down ) /* draw a red (or white) stop light */
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gStopRect);
- ForeColor(blackColor);
- FrameOval(&gStopRect);
- if ( ! down ) /* draw a green (or white) go light */
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gGoRect);
- ForeColor(blackColor);
- FrameOval(&gGoRect);
-
- }
- }
- else
- {
- if (down)
- {
- down = false;
- if ( down ) /* draw a red (or white) stop light */
- ForeColor(redColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gStopRect);
- ForeColor(blackColor);
- FrameOval(&gStopRect);
- if ( ! down ) /* draw a green (or white) go light */
- ForeColor(greenColor);
- else
- ForeColor(whiteColor);
- PaintOval(&gGoRect);
- ForeColor(blackColor);
- FrameOval(&gGoRect);
-
- }
- gLastDown = TickCount();
- };
-
- {
- WindowPtr firstWindow, secondWindow;
- Rect destRect, srcRect;
-
- firstWindow = FrontWindow();
- SetPort(firstWindow);
-
- SetRect(&srcRect,0,0,250,150);
- destRect = srcRect;
- OffsetRect(&destRect, 10, 10);
- OffsetRect(&srcRect, lastValue * 250, 0);
- CopyBits((BitMapPtr) *(GetGWorldPixMap(myGWorld)),&((qd.thePort)->portBits),&srcRect,&destRect,srcCopy,0L);
-
- }
- }
-